<template>
	<div class="app-container">
		<!-- 查询和其他操作 -->
		<div class="filter-container">
			<el-select v-model="listQuery.type" style="width: 200px" class="filter-item" placeholder="请选择管理员类型">
				<el-option v-for="(key, value) in typeDic" :key="key" :label="key" :value="value"/>
			</el-select>
			<el-input v-model="listQuery.nickName" clearable class="filter-item" style="width: 200px;"
			          placeholder="请输入昵称"/>
			<el-button v-permission="[1]" class="filter-item" type="primary" icon="el-icon-search"
			           @click="handleFilter">
				查找
			</el-button>
			<el-button v-permission="[1]" class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">
				添加
			</el-button>
		</div>

		<!-- 查询结果 -->
		<el-table v-loading="listLoading" :data="list" size="small" element-loading-text="正在查询中。。。" border fit
		          highlight-current-row>
			<el-table-column align="center" width="100px" label="管理员ID" prop="id" sortable/>

			<el-table-column align="center" label="登录名" prop="loginName"/>
			<el-table-column align="center" label="昵称" prop="nickName"/>

			<el-table-column align="center" width="120px" label="手机号码" prop="phone"/>

			<el-table-column align="center" width="120px" label="用户类型" prop="type">
				<template slot-scope="scope">
					<el-tag>{{ scope.row.type | typeDicFilter }}</el-tag>
				</template>
			</el-table-column>

			<el-table-column align="center" label="创建时间" prop="createdTime">
				<template slot-scope="scope">{{ scope.row.createdTime | formatTime }}</template>
			</el-table-column>
			x
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
		            @pagination="getList"/>


		<!-- 添加或修改对话框 -->
		<el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
			<el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left"
			         label-width="100px" style="width: 400px; margin-left:50px;">
				<el-form-item label="登录名" prop="loginName">
					<el-input v-model="dataForm.loginName"/>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input v-model="dataForm.password"/>
				</el-form-item>
				<el-form-item label="昵称" prop="nickName">
					<el-input v-model="dataForm.nickName"/>
				</el-form-item>
				<el-form-item label="手机号" prop="phone">
					<el-input v-model="dataForm.phone"/>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">取消</el-button>
				<el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
				<el-button v-else type="primary" @click="updateData">确定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {listRole, createRole, updateRole, deleteRole} from '@/api/role'
	import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

	const typeDic = {
		1: '平台管理员',
		2: '商家管理员'
	}
	export default {
		name: 'User',
		components: {Pagination},
		filters: {
			typeDicFilter(code) {
				return typeDic[code]
			}
		},
		data() {
			return {
				list: undefined,
				total: 0,
				listLoading: true,
				listQuery: {
					pageNum: 1,
					pageSize: 20,
					type: undefined
				},
				dataForm: {
					loginName: undefined,
					password: undefined,
					nickName: undefined,
					phone: undefined
				},
				dialogFormVisible: false,
				dialogStatus: '',
				textMap: {
					update: '编辑',
					create: '创建'
				},
				rules: {
					loginName: [{required: true, message: '登录名不能为空', trigger: 'blur'}],
					password: [{required: true, message: '密码不能为空', trigger: 'blur'}]
				},
				typeDic
			}
		},
		created() {
			this.getList()
		},
		methods: {
			getList() {
				this.listLoading = true
				listRole(this.listQuery)
					.then(res => {
						this.list = res.data.records
						this.total = res.data.total
						this.listLoading = false

						console.log(this.list)
					})
					.catch(() => {
						this.list = []
						this.total = 0
						this.listLoading = false
					})
			},
			resetForm() {
				this.dataForm = {
					id: undefined,
					level: undefined,
					phone: undefined,
					gender: undefined,
					nickname: undefined,
					birthday: undefined,
					status: undefined
				}
			},
			handleFilter() {
				this.listQuery.pageNum = 1
				this.getList()
			},
			handleCreate() {
				this.resetForm()
				this.dialogStatus = 'create'
				this.dialogFormVisible = true
				this.$nextTick(() => {
					this.$refs['dataForm'].clearValidate()
				})
			},
			createData() {
				this.$refs['dataForm'].validate(valid => {
					if (valid) {
						createRole(this.dataForm)
							.then(res => {
								this.list.unshift(res.data.data)
								this.dialogFormVisible = false
								this.$notify.success({
									title: '成功',
									message: '创建成功'
								})
								this.getList()
							})
							.catch(res => {
								this.$notify.error({
									title: '失败',
									message: res.data.errmsg
								})
							})
					}
				})
			},
			handleUpdate(row) {
				this.dataForm = Object.assign({}, row)
				this.dialogStatus = 'update'
				this.dialogFormVisible = true
				this.$nextTick(() => {
					this.$refs['dataForm'].clearValidate()
				})
			},
			updateData() {
				this.$refs['dataForm'].validate(valid => {
					if (valid) {
						updateRole(this.dataForm)
							.then(() => {
								for (const v of this.list) {
									if (v.id === this.dataForm.id) {
										const index = this.list.indexOf(v)
										this.list.splice(index, 1, this.dataForm)
										break
									}
								}
								this.dialogFormVisible = false
								this.$notify.success({
									title: '成功',
									message: '更新用户成功'
								})
							})
							.catch(response => {
								this.$notify.error({
									title: '失败',
									message: response.data.errmsg
								})
							})
					}
				})
			}
		}
	}
</script>
